package org.ceaport.rest.server;

import org.ceaport.rest.server.events.ChatEventStream;
import org.ceaport.rest.server.events.EventStream;
import org.ceaport.rest.server.events.MeetMeEventStream;

import org.restlet.Application;
import org.restlet.Component;
import org.restlet.Context;
import org.restlet.Restlet;
import org.restlet.Router;
import org.restlet.data.Protocol;

/**
 * CEAport framework to create open APIs for communications enable applications.
 * https://ceaport.googlecode.com
 *
 * Copyright (c) 2008 by respective authors (see below).
 *
 * This program is free software; you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License as published by the Free Software
 * Foundation; either version 2.1 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 * 
 * Contributors:
 * Michael Weiss (michael.r.weiss@gmail.com)
 * 
 */
public class ConferenceApplication extends Application {

	public ConferenceApplication(Context context) {
		super(context);
	}
	
	@Override
	public synchronized Restlet createRoot() {
		// create a router that routes each call to the respective restlet
		Router router = new Router(getContext());
		
		// define routes
		router.attach("/conference/{conference}", ConferenceResource.class);
		router.attach("/conference/{conference}/chat/message/{message}", ChatResource.class);
		router.attach("/conference/{conference}/user/{user}", UserResource.class);
	
		return router;
	}
	
	/**
	 * Run stand-alone restlet server.
	 * @param args
	 */
	public static void main(String[] args) {
		// create a component
		Component component = new Component();
		
		// add an HTTP server
		component.getServers().add(Protocol.HTTP, 8112);
		
		// create application
		ConferenceApplication application = new ConferenceApplication(component.getContext());
		
		// set attributes for application context
		if (args.length >= 2) {
			String host = args[0];
			String room = args[1];
			Database db = new Database();
			Conference conference = new Conference(host, room);
			db.setConference(room, conference);
			
			// set up to listen for events
			EventStream meetMeEvents = new MeetMeEventStream(host, room);
			meetMeEvents.addEventConsumer(db);
			
			EventStream chatEvents = new ChatEventStream(host, room);
			chatEvents.addEventConsumer(db);
			
			// component context does not get passed on downstream, so do as suggested here:
			// http://osdir.com/ml/java.restlet/2007-04/msg00165.html
			application.getContext().getAttributes().put("db", db);
		}
		
		// attach application
		component.getDefaultHost().attach(application);
		
		// start the component
		try {
			component.start();
		} catch (Exception e) {
			// something went wrong
			e.printStackTrace();
		}
	}
}
